import "../css/index.scss";
import Render from "./render";
import "./request";

const u = require("./util.js")
let all_data = [];

// declare main function
function get_config() {
    get("/config", {}).then(e => {
        slider_bar.for(e.slider);
        home_notice.obj(e);
        u.default.registerEvent(".slider-bar-item", function (target) {
            let url = "/";
            switch (target.target.dataset.id) {
                case "0":
                    url += "";
                    break;
                case "1":
                    url += "note";
                    break;
                case "2":
                    url += "to";
                    break;
            }
            u.default.go(url);
        })
    });
}

u.default.registerEvent(".is_continue", function (target) {
    get_article({page: target.target.dataset.page}, 1);
})

function get_article(data = {}, type = 0, reset = false) {
    if (reset) {
        all_data = [];
    }
    get("/api/article/get", data).then(e => {
        if (type === 0) {
            all_data = e.data;
        } else {
            all_data = [...all_data, ...e.data];
        }
        content_main.for(all_data);
        is_continue.obj(e);
        u.default.registerEvent(".list-title", function (target) {
            u.default.go("/detail/" + target.target.dataset.id);
        })
    });
}

function get_category(data = {}) {
    get("/api/category/get", data).then(e => {
        category.for(e);
        u.default.registerEvent(".category-main-list-item", function (target) {
            // u.default.go("/category/")
            get_article({op: "category", data: target.target.dataset.id}, 0, true)
        })
    })
}

// change the page size
u.default.changeH("container", window.innerHeight)
u.default.changeW("container", document.innerWidth);
// declare area variables
let slider_bar = new Render(".slider-bar-list");
let content_main = new Render(".content-main");
let home_notice = new Render(".home-notice");
let category = new Render(".category-main-list");
let is_continue = new Render(".is_continue");
// render the data to html
get_config();
get_article();
get_category();

// tool
function pageScroll(speed = 1.0) {
    speed += 0.3;
    window.scrollBy(0, -15 - speed);
    let scrolldelay = setTimeout(function () {
        pageScroll(speed)
    }, 1);
    let sTop = document.documentElement.scrollTop + document.body.scrollTop;
    if (sTop <= 0) clearTimeout(scrolldelay);
}

u.default.registerEvent(".up", e => {
    pageScroll();
});


